package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.mapper.DesignModuleMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleDetailsService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DesignModuleServiceImpl extends ServiceImpl<DesignModuleMapper, DesignModule>
    implements DesignModuleService {

    @Autowired
    private DesignModuleMapper designModuleMapper;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;


    /**
     * 获取Id最大物料设计单的设计单的编号
     * @return
     */
    @Override
    public String getMaxDesignId() {
        DesignModule designModule = designModuleMapper.queryMaxModule();
        if(designModule==null){
            return null;
        }
        //获取设计单编号
        String designId = designModule.getDesignId();
        return designId;
    }

    /**
     * 完成物料设计单
     * @param designModuleDto
     */
    @Override
    @Transactional
    public boolean addMaterial(DesignModuleDto designModuleDto) {
        // 1. 插入物料设计单
        this.save(designModuleDto);

        // 2. 插入物料设计单明细
        List<DesignModuleDetails> details =
                designModuleDto.getDetails();
        for (int i=0; i<details.size(); i++) {
            DesignModuleDetails detail = details.get(i);
            // 每条明细id置为null
            detail.setId(null);
            // 获取物料设计单的id
            Integer parentId = designModuleDto.getId();
            // 设置父级序号
            detail.setParentId(parentId);
            // 设置物料序号
            detail.setDetailsNumber(i+1);
            // 设置可用数量
            detail.setResidualAmount(detail.getAmount());
            // 插入物料明细
            designModuleDetailsService.save(detail);
        }

        // 3. 修改产品档案的物料设计标志 W001-1: 已设计
        String productId = designModuleDto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        designFile.setDesignModuleTag("W001-1");
        boolean flag = designFileService.updateById(designFile);
        return flag;
    }

    @Override
    public PageUtils<DesignModule> auditPage(DesignModuleVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<DesignModule> queryWrapper = new QueryWrapper<>();
        //查询条件判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("FIRST_KIND_ID",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("SECOND_KIND_ID",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("THIRD_KIND_ID",vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_reviewStatus())){
                queryWrapper.eq("CHECK_TAG",vo.getSearch_reviewStatus());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("REGISTER_TIME",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("REGISTER_TIME",vo.getSearch_registerTime2());
            }
        }
        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<DesignModule> designModules = designModuleMapper.selectList(queryWrapper);
        PageInfo<DesignModule> pageInfo=new PageInfo<>(designModules);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignModule> pageUtils=new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());

        return pageUtils;
    }
}
